package com.yndsoft.jc.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.Ad;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.OrderDet;
import com.yndsoft.jc.model.Product;
import com.yndsoft.jc.model.ProductAttribute;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.categorypage.adapter.ProductDetailAdapter;
import com.yndsoft.jc.ui.memberpage.Session;
import com.yndsoft.jc.ui.memberpage.login.LoginActivity;
import com.yndsoft.jc.ui.net.HttpUtil;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.ui.shoppingcart.ConfirmOrderActivity;
import com.yndsoft.jc.ui.shoppingcart.entity.CartBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/8/27.
 */

public class CustomDialog extends ProgressDialog implements View.OnClickListener, ProductDetailAdapter.CheckInterface {

    List<ProductAttribute> list;
    ProductDetailAdapter adapter;
    Product product;
    List<ProductAttribute> resultList;
    AddToCart addToCart;
    HttpUtil mHttpUtil;
    Context context;
    int productID;
    boolean toCart;//加入购物车还是直接进入订单确认
    Session mSession;

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme, Product product, List<ProductAttribute> list, HttpUtil mHttpUtil, int productID, Session mSession) {
        super(context, theme);
        this.context = context;
        this.product = product;
        this.list = list;
        this.mHttpUtil = mHttpUtil;
        this.productID = productID;
//        this.toCart = toCart;
        this.mSession = mSession;
    }

    public boolean isToCart() {
        return toCart;
    }

    public void setToCart(boolean toCart) {
        this.toCart = toCart;
    }

    public void setAddToCart(AddToCart addToCart) {
        this.addToCart = addToCart;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init(getContext(), list);
    }

    public void setList(List<ProductAttribute> list) {
//        this.list = list;
        adapter.setData(list);
    }

    private void init(Context context, List<ProductAttribute> list) {
        //dialog弹出后会点击屏幕或物理返回键，dialog不消失
//        setCancelable(true);
        //dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        setCanceledOnTouchOutside(true);

        setContentView(R.layout.my_custom_dialog);
        ImageView dialog_closeBtn = findViewById(R.id.dialog_closeBtn);
        ListView product_detail_list = findViewById(R.id.product_detail_list);
        Button product_confirm = findViewById(R.id.product_confirm);


        adapter = new ProductDetailAdapter(context);
        adapter.setCheckInterface(this);
        product_detail_list.setAdapter(adapter);
        adapter.setData(list);

        dialog_closeBtn.setOnClickListener(this);
        product_confirm.setOnClickListener(this);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.dimAmount = 0.5f;
        getWindow().setAttributes(params);

//        Glide.with(context).load(R.drawable.loading).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(lottieAnimationView);
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    private Order order;
    private List<OrderDet> detList = new ArrayList<>();

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_closeBtn:
                clearBg();
                dismiss();
                break;
            case R.id.product_confirm://确认
                if (mSession.isLogin()) {
                    resultList = new ArrayList<>();
                    order = new Order();
                    for (int i = 0; i < list.size(); i++) {
                        ProductAttribute productAttribute = list.get(i);
                        if (productAttribute.isChoose()) {
                            resultList.add(productAttribute);
                        }
                    }
                    if (isToCart()) {//加入购物车
                        postProduct();
                    } else {//直接进入订单确认
                        detList.clear();
                        for (int x = 0; x < resultList.size(); x++) {
                            OrderDet orderDet = new OrderDet();
                            orderDet.setProduct(product);
                            orderDet.setProductattribute(resultList.get(x));
                            detList.add(orderDet);
                        }
                        order.setDets(detList);
                        Intent intent = new Intent(context, ConfirmOrderActivity.class);
                        intent.putExtra("MyOrder", order);
                        intent.putExtra("ctype", 0);
                        context.startActivity(intent);
                        dismiss();
                    }
                } else {
                    context.startActivity(new Intent(context, LoginActivity.class));
                }
                clearBg();
//                addToCart.addToShopCart(resultList);
                break;
        }
    }

    private void clearBg() {
        for (int x = 0; x < list.size(); x++) {
            list.get(x).setChoose(false);
        }
    }

    private void postProduct() {
//        SharedPreferences sp = context.getSharedPreferences(context.getString(R.string.sp_member), Context.MODE_PRIVATE);
//        int mid = sp.getInt(sp.getString(context.getString(R.string.sp_jc), ""), 100001);
        List<CartBean> postList = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            CartBean cartBean = new CartBean();
            cartBean.setMid(mSession.getUserInfo().getId());
            cartBean.setPid(productID);
            cartBean.setNum(1);
            cartBean.setPaid(resultList.get(i).getId());
            postList.add(cartBean);
        }
        mHttpUtil.addToCart(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(context, "添加失败！");
                        break;
                    case 1:
                        ToastUtil.show(context, "添加到购物车！");
                        dismiss();
                        break;
                }
            }
        }, "tocart", mSession.getUserInfo().getId(), new Gson().toJson(postList));
    }

    @Override
    public void checkGroup(int position, boolean isChecked) {
        list.get(position).setChoose(isChecked);
        adapter.notifyDataSetChanged();
    }

    public interface AddToCart {
        public void addToShopCart(List<ProductAttribute> list);
    }
}